import xcms.DataBus;
import xcms.gui.CommonBuilder;
import xcms.plugins.auth.LoginForm;

/**
 * Plugin responsible for authentication.
 */
class xcms.plugins.auth.AuthPlugin
{
    /**
     * Initializes the plugin.
     */
    this.AuthPlugin = function() {
        xcms.subscribe('onCmsInit', this);
    }

    /**
     * Calls when CMS application is being initialized.
     */
    this.onCmsInit = function() {
        // Create special icon at the bottom of page
        var builder = new xcms.gui.CommonBuilder();
        this.icon = builder.createLink(null, '[ CMS ]', this, -10, -10);
        this.icon.show();
    }

    /**
     * Calls when user clicks CMS icon.
     */
    this.onActionPerformed = function(icon) {
        var form = LoginForm.getForm();
        form.addFormListener(this);
        if (form.isVisible()) {
            form.hide();
        } else {
            form.move(icon.getAbsX() - form.getWidth(), icon.getAbsY() - form.getHeight());
            form.show();
        }
    }

    /**
     * Calls when user submitted login form.
     *
     * @param form (LoginForm) Login form reference.
     */
    this.onFormComplete = function(form) {
        console.info('Successfully logged in');
        xcms.publish('onAuthComplete');
        this.icon.hide();
        form.hide();
    }

    /**
     * Calls when user cancelled login form.
     *
     * @param form (LoginForm) Login form reference.
     */
    this.onFormCancelled = function(form) {
        form.hide();
    }
}